home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / runtime / mlvalues.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-24  |  7.0 KB  |  223 lines  |  [TEXT/MPS ]

  1. #ifndef _mlvalues_
  2. #define _mlvalues_
  3.  
  4.  
  5. #include "config.h"
  6.  
  7. /* Definitions
  8.  
  9.   word: Four bytes on 32 and 16 bit architectures,
  10.         eight bytes on 64 bit architectures.
  11.   long: A C long integer.
  12.   val: The ML representation of something.  A long or a block or a pointer
  13.        outside the heap.  If it is a block, it is the (encoded) address
  14.        of an object.  If it is a long, it is encoded as well.
  15.   object: Something allocated.  It always has a header and some
  16.           fields or some number of bytes (a multiple of the word size).
  17.   field: A word-sized val which is part of an object.
  18.   bp: Pointer to the first byte of an object.  (a char *)
  19.   op: Pointer to the first field of an object.  (a value *)
  20.   hp: Pointer to the header of an object.  (a char *)
  21.   int32: Four bytes on all architectures.
  22.  
  23.   Remark: An object size is always a multiple of the word size, and at least
  24.           one word plus the header.
  25.  
  26.   bosize: Size (in bytes) of the "bytes" part.
  27.   wosize: Size (in words) of the "fields" part.
  28.   bhsize: Size (in bytes) of the object with its header.
  29.   whsize: Size (in words) of the object with its header.
  30.  
  31.   hd: A header.
  32.   tag: The value of the tag field of the header.
  33.   color: The value of the color field of the header.
  34.          This is for use only by the GC.
  35. */
  36.  
  37. typedef long value;
  38. typedef unsigned long header_t;
  39. #ifdef SIXTEEN
  40. typedef unsigned int mlsize_t;
  41. #else
  42. typedef unsigned long mlsize_t;
  43. #endif
  44. typedef unsigned int tag_t;             /* Actually, an unsigned char */
  45. typedef unsigned long color_t;
  46. typedef unsigned long mark_t;
  47.  
  48. #ifdef SIXTYFOUR
  49. typedef int int32;            /* Not portable, but checked by autoconf. */
  50. typedef unsigned int uint32;  /* Seems like a reasonable assumption anyway. */
  51. #else
  52. typedef long int32;
  53. typedef unsigned long uint32;
  54. #endif
  55.  
  56. /* Longs vs blocks. */
  57. #define Is_long(x)   (((x) & 1) == 1)
  58. #define Is_block(x)  (((x) & 1) == 0)
  59.  
  60. /* Conversion macro names are always of the form  "to_from". */
  61. /* Example: Val_long as in "Val from long" or "Val of long". */
  62. #define Val_long(x)     (((long)(x) << 1) + 1)
  63. #define Long_val(x)     ((x) >> 1)
  64. #define Max_long ((1L << (8 * sizeof(value) - 2)) - 1)
  65. #define Min_long (-(1L << (8 * sizeof(value) - 2)))
  66. #define Val_int Val_long
  67. #define Int_val(x) ((int) Long_val(x))
  68.  
  69. /* Structure of the header:
  70.  
  71. For 16-bit and 32-bit architectures:
  72.      +--------+-------+-----+
  73.      | wosize | color | tag |
  74.      +--------+-------+-----+
  75. bits  31    10 9     8 7   0
  76.  
  77. For 64-bit architectures:
  78.  
  79.      +--------+-------+-----+
  80.      | wosize | color | tag |
  81.      +--------+-------+-----+
  82. bits  63    10 9     8 7   0
  83.  
  84. */
  85.  
  86. #define Tag_hd(hd) ((tag_t) ((hd) & 0xFF))
  87. #define Wosize_hd(hd) ((mlsize_t) ((hd) >> 10))
  88.  
  89. #define Hd_val(val) (((header_t *) (val)) [-1])        /* Also an l-value. */
  90. #define Hd_op(op) (Hd_val (op))                        /* Also an l-value. */
  91. #define Hd_bp(bp) (Hd_val (bp))                        /* Also an l-value. */
  92. #define Hd_hp(hp) (* ((header_t *) (hp)))              /* Also an l-value. */
  93. #define Hp_val(val) ((char *) (((header_t *) (val)) - 1))
  94. #define Hp_op(op) (Hp_val (op))
  95. #define Hp_bp(bp) (Hp_val (bp))
  96. #define Val_op(op) ((value) (op))
  97. #define Val_hp(hp) ((value) (((header_t *) (hp)) + 1))
  98. #define Op_hp(hp) ((value *) Val_hp (hp))
  99. #define Bp_hp(hp) ((char *) Val_hp (hp))
  100.  
  101. #define Num_tags (1 << 8)
  102. #ifdef SIXTYFOUR
  103. #define Max_wosize ((1L << 54) - 1)
  104. #else
  105. #ifdef SIXTEEN
  106. #define Max_wosize ((1 << 14) - 1)
  107. #else
  108. #define Max_wosize ((1 << 22) - 1)
  109. #endif
  110. #endif
  111.  
  112. #define Wosize_val(val) (Wosize_hd (Hd_val (val)))
  113. #define Wosize_op(op) (Wosize_val (op))
  114. #define Wosize_bp(bp) (Wosize_val (bp))
  115. #define Wosize_hp(hp) (Wosize_hd (Hd_hp (hp)))
  116. #define Whsize_wosize(sz) ((sz) + 1)
  117. #define Wosize_whsize(sz) ((sz) - 1)
  118. #define Wosize_bhsize(sz) ((sz) / sizeof (value) - 1)
  119. #define Bsize_wsize(sz) ((sz) * sizeof (value))
  120. #define Wsize_bsize(sz) ((sz) / sizeof (value))
  121. #define Bhsize_wosize(sz) (Bsize_wsize (Whsize_wosize (sz)))
  122. #define Bhsize_bosize(sz) ((sz) + sizeof (header_t))
  123. #define Bosize_val(val) (Bsize_wsize (Wosize_val (val)))
  124. #define Bosize_op(op) (Bosize_val (Val_op (op)))
  125. #define Whsize_hp(hp) (Whsize_wosize (Wosize_hp (hp)))
  126. #define Whsize_val(val) (Whsize_hp (Hp_val (val)))
  127. #define Bhsize_hp(hp) (Bsize_wsize (Whsize_hp (hp)))
  128.  
  129. #ifdef BIG_ENDIAN
  130. #define Tag_val(val) (((unsigned char *) (val)) [-1])
  131.                                                  /* Also an l-value. */
  132. #define Tag_hp(hp) (((unsigned char *) (hp)) [sizeof(value)-1])
  133.                                                  /* Also an l-value. */
  134. #else
  135. #define Tag_val(val) (((unsigned char *) (val)) [-sizeof(value)])
  136.                                                  /* Also an l-value. */
  137. #define Tag_hp(hp) (((unsigned char *) (hp)) [0])
  138.                                                  /* Also an l-value. */
  139. #endif
  140.  
  141. /* The Lowest tag for blocks containing no value. */
  142. #define No_scan_tag (Num_tags - 4)
  143.  
  144.  
  145. /* 1- If tag < No_scan_tag : a tuple of fields.  */
  146.  
  147. /* Pointer to the first field. */
  148. #define Op_val(x) ((value *) (x))
  149. /* Fields are numbered from 0. */
  150. #define Field(x, i) (((value *)(x)) [i])           /* Also an l-value. */
  151.  
  152. typedef unsigned char *code_t;
  153.  
  154. #define Closure_wosize 2
  155. #define Closure_tag (No_scan_tag - 1)
  156. #define Code_val(val) (((code_t *) (val)) [0])     /* Also an l-value. */
  157. #define Env_val(val) (Field(val, 1))               /* Also an l-value. */
  158.  
  159.  
  160. /* 2- If tag >= No_scan_tag : a sequence of bytes. */
  161.  
  162. /* Pointer to the first byte */
  163. #define Bp_val(v) ((char *) (v))
  164. #define Val_bp(p) ((value) (p))
  165. /* Bytes are numbered from 0. */
  166. #define Byte(x, i) (((char *) (x)) [i])            /* Also an l-value. */
  167. #define Byte_u(x, i) (((unsigned char *) (x)) [i]) /* Also an l-value. */
  168.  
  169. /* Abstract things.  Their contents is not traced by the GC; therefore they
  170.    must not contain any [value].
  171. */
  172. #define Abstract_tag No_scan_tag
  173.  
  174. /* Strings. */
  175. #define String_tag (No_scan_tag + 1)
  176. #define String_val(x) ((char *) Bp_val(x))
  177.  
  178. /* Floating-point numbers. */
  179. #define Double_tag (No_scan_tag + 2)
  180. #define Double_wosize ((sizeof(double) / sizeof(value)))
  181. #ifndef ALIGN_DOUBLE
  182. #define Double_val(v) (* (double *) (v))
  183. #define Store_double_val(v,d) (* (double *) (v) = (d))
  184. #else
  185. #ifdef ANSI
  186. extern double Double_val(value);
  187. extern void Store_double_val(value,double);
  188. #else
  189. double Double_val();
  190. void Store_double_val();
  191. #endif
  192. #endif
  193.  
  194. /* Finalized things.  Just like abstract things, but the GC will call the
  195.    [Final_fun] before deallocation.
  196. */
  197. #define Final_tag (No_scan_tag + 3)
  198. #ifdef ANSI
  199. typedef void (*final_fun) (value);
  200. #else
  201. typedef void (*final_fun) ();
  202. #endif
  203. #define Final_fun(val) (((final_fun *) (val)) [0]) /* Also an l-value. */
  204.  
  205.  
  206. /* 3- Atoms are 0-tuples.  They are statically allocated once and for all. */
  207.  
  208. extern header_t first_atoms[];
  209. #define Atom(tag) (Val_hp (&(first_atoms [tag])))
  210.  
  211. /* Booleans are atoms tagged 0 or 1 */
  212.  
  213. #define Val_bool(x) Atom((x) != 0)
  214. #define Bool_val(x) Tag_val(x)
  215. #define Val_false Atom(0)
  216. #define Val_true Atom(1)
  217.  
  218. /* The unit value is the atom tagged 0 */
  219.  
  220. #define Val_unit Atom(0)
  221.  
  222. #endif /* _mlvalues_ */
  223.